package com.qigao.mall.service.impl;

import com.qigao.mall.api.ShopInfoService;
import com.qigao.mall.api.ShopRecentService;
import com.qigao.mall.commons.dto.ShopInfoDto;
import com.qigao.mall.commons.dto.ShopRecentDto;
import com.qigao.mall.commons.po.UserInfo;
import com.qigao.mall.jdbc.mapper.TbShopRecentMapper;
import com.qigao.mall.jdbc.model.TbShopRecent;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ShopRecentServiceImpl implements ShopRecentService {
    @Resource
    private TbShopRecentMapper tbShopRecentMapper;
    @Resource
    private ShopInfoService shopInfoService;
    @Override
    public List<ShopRecentDto> findByUserNo(String userNo) {
        List<TbShopRecent> tbShopRecents= tbShopRecentMapper.selectByUserNo(userNo);
        if(tbShopRecents==null || tbShopRecents.size()<=0){
            return null;
        }
        List<ShopRecentDto> shopRecentDtos=new ArrayList<>();
        for(TbShopRecent tbShopRecent:tbShopRecents){
            ShopRecentDto shopRecentDto=new ShopRecentDto();
            BeanUtils.copyProperties(tbShopRecent,shopRecentDto);
            shopRecentDtos.add(shopRecentDto);
        }
        return shopRecentDtos;
    }

    @Override
    public void saveShopRecent(String shopNo, UserInfo userInfo) {
        ShopInfoDto shopInfo=shopInfoService.findByShopNo(shopNo);
        TbShopRecent tbShopRecent=new TbShopRecent();
        tbShopRecent.setShopNo(shopNo);
        tbShopRecent.setShopName(shopInfo.getShopName());
        tbShopRecent.setUserNo(userInfo.getUserNo());
        tbShopRecent.setUserName(userInfo.getUserName());
        tbShopRecent.setCreateTime(new Date());
        tbShopRecent.setModiTime(new Date());
        TbShopRecent tbShopRecent1= tbShopRecentMapper.selectByIndex(tbShopRecent.getShopNo(),tbShopRecent.getUserNo());
        if(tbShopRecent1!=null){
            tbShopRecent1.setModiTime(new Date());
            tbShopRecentMapper.updateByPrimaryKeySelective(tbShopRecent1);
        }else {
            tbShopRecentMapper.insertSelective(tbShopRecent);
        }
    }
}
